

package cn.shengchao.examstar.pay.acl.adapters.client;

import cn.shengchao.examstar.pay.acl.ports.client.IProductClient;
import cn.shengchao.examstar.pay.acl.ports.client.ProductPayPrice;
import cn.shengchao.examstar.pay.model.entity.ProductEntity;
import cn.shengchao.examstar.product.domain.entity.FreeCourseKnowledgeProduct;
import cn.shengchao.examstar.product.ohs.local.appservice.ProductAppService;
import cn.shengchao.examstar.product.ohs.local.message.ProductDto;
import cn.shengchao.examstar.product.ohs.local.message.ProductPayPriceDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ProductClient implements IProductClient {

    @Autowired
    private ProductAppService productAppService;

    @Override
    public ProductEntity queryProductByProductId(String productId) {
        ProductDto productDto = productAppService.getProductById(productId);
        return ProductEntity.builder()
                .productId(productDto.getProductId())
                .productName(productDto.getProductName())
                .productDesc(productDto.getProductDesc())
                .price(productDto.getPrice())
                .build();
    }

    @Override
    public ProductPayPrice calculateProductPayPrice(String productId, List<Long> subProductIds) {
        ProductPayPriceDto productPayPrice = productAppService.calculateProductPayPrice(productId, subProductIds);
        ProductPayPrice productPayPriceDto = new ProductPayPrice();
        productPayPriceDto.setProduct(ProductEntity.builder()
                .price(productPayPrice.getProduct().getPrice())
                .productName(productPayPrice.getProduct().getProductName())
                .discountPrice(productPayPrice.getProduct().getDiscountPrice())
                .productDesc(productPayPrice.getProduct().getProductDesc())
                .productType(productPayPrice.getProduct().getProductType())
                .productId(productPayPrice.getProduct().getProductId())
                .build());
        if (productPayPrice.getSubProducts() != null) {
            productPayPriceDto.setSubProducts(productPayPrice.getSubProducts()
                    .stream()
                    .map(item -> ProductEntity.builder()
                            .price(item.getPrice())
                            .productName(item.getProductName())
                            .discountPrice(item.getDiscountPrice())
                            .productDesc(item.getProductDesc())
                            .productId(item.getProductId())
                            .build())
                    .toList());
        }
        productPayPriceDto.setPayPrice(productPayPrice.getPayPrice());
        productPayPriceDto.setDiscountInfo(productPayPrice.getDiscountInfo());
        return productPayPriceDto;
    }

    @Override
    public FreeCourseKnowledgeProduct loadFreeProductNode(Long freeCourseNodeId) {
        return productAppService.loadFreeProduct(freeCourseNodeId);
    }
}
